<?php

declare(strict_types=1);

namespace App\Controller\Admin\V1\Admin;

use App\Controller\Admin\V1\Controllers;
use App\Request\Admin\Admin as AdminRequest;
use App\Services\Admin\Admin as AdminServices;
use App\Services\Admin\Role as RoleServices;
use function Hyperf\Translation\__;

/**
 * 管理员管理
 * @package App\Controller\Admin\V1\Admin
 */
class Admin extends Controllers
{
    /**
     * Admin constructor.
     * @param AdminServices $services
     */
    public function __construct(
        AdminServices $services,
        protected RoleServices $roleServices
    )
    {
        parent::__construct();

        $this->services = $services;
    }

    /**
     * 分页
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function page()
    {
        $get = $this->request->query();

        [$field, $order] = $this->orderMap();
        $page = $this->services
            ->search($get)
            ->orderBy($field, $order)
            ->paginate($this->getLimit());

        return success([
            'page' => $page
        ]);
    }

    /**
     * 添加
     * @param AdminRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function save(AdminRequest $request)
    {
        $input = $request->only([
            'head_img',
            'nickname',
            'username',
            'password',
            'mobile',
            'email',
            'is_admin',
            'status',
            'role'
        ]);

        if (getInfo('admin', 'is_admin') == 0) {
            $input['is_admin'] = 0;
        }

        $model = $this->services->create($input);
        if (!$model->exists) {
            return internalError(__('message.stored.failed'));
        }

        $model->addRole($input['role']);

        return stored();
    }

    /**
     * 编辑
     * @param int $id
     * @param AdminRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function update(int $id, AdminRequest $request)
    {
        $model = $this->services->findOrFail($id);
        $input = $request->only([
            'head_img',
            'nickname',
            'password',
            'mobile',
            'email',
            'is_admin',
            'status',
            'role'
        ]);

        if (getInfo('admin', 'is_admin') == 0) {
            $input['is_admin'] = 0;
            $input['role'] = $this->roleServices->whereIn('id', $input['role'])->pluck('id')->toArray();
            if (!$input['role']) {
                return notfound(__('message.role.not_found'));
            }
        }

        if (!$model->fill($input)->save()) {
            return internalError(__('message.updated.failed'));
        }

        $model->updateRole($input['role']);

        return updated();
    }

    /**
     * 批量删除
     * @param AdminRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Exception
     */
    public function destroy(AdminRequest $request)
    {
        $input = $request->only(['ids']);

        $this->services
            ->whereIn('id', $input['ids'])
            ->where('is_admin', 0)
            ->delete();

        return deleted();
    }
}
